package com.lhj.springboot3.core.service;

import com.lhj.springboot3.core.bean.UserEntity;
import com.lhj.springboot3.core.event.LoginSuccessEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.EventListener;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Description
 * @Author lhj
 * @Date 2024/6/30 10:32
 */
@Order(2)
@Slf4j
@Service
public class LoginLoggerService implements ApplicationListener<LoginSuccessEvent> {
    
    public void recordLoginLog(String username){
        log.info("登陆用户: {}, 登陆时间: {}", username, new Date().toString());
    }

    @Override
    public void onApplicationEvent(LoginSuccessEvent event) {
        log.info("登陆成功 事件监听器: {}", event.toString());
        UserEntity source = (UserEntity) event.getSource();
        log.info("用户: {}, 登陆成功, 登陆时间: {}", source.getUserName(), new Date().toString());
    }
}
